class ListNode {
    val: number;
    next: ListNode | null;
    constructor(val?: number, next?: ListNode | null) {
        this.val = val === undefined ? 0 : val;
        this.next = next === undefined ? null : next;
    }
}
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
    const dummy = new ListNode(0);//创建哑节点，简化头节点删除的情况
    dummy.next = head;

    let slow: ListNode|null = dummy;
    let fast: ListNode|null = dummy;

    // 快指针先走n+1步
    for (let i = 0; i < n; i++) {
        fast = fast!.next;
    }

    // 快慢指针一起走，当快指针到达链表尾部时，慢指针指向倒数第n+1个节点
    while (fast) {
        slow = slow!.next;
        fast = fast.next;
    }

    // 删除倒数第n个节点
    slow!.next =  slow!.next!.next;

    return dummy.next;// 返回哑节点的下一个节点，即删除节点后的链表头节点
};